package response

import (
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/foundation/plat-echo/app/ops/models/request"
)

type TemplateResp struct {
	ID            uint64                   `form:"id" json:"id,omitempty"`
	Alert         *models.Alert            `form:"alert"  json:"alert"`
	TypeName      string                   `form:"type_name"  json:"type_name,omitempty"`
	Subscribe     bool                     `form:"subscribe"  json:"subscribe,omitempty"`
	SubscribeInfo *request.SubscribeInfo   `form:"subscribe_info"  json:"subscribe_info,omitempty"`
	ShowSubscribe bool                     `form:"show_subscribe"  json:"show_subscribe,omitempty"`
	General       *request.TemplateGeneral `form:"general" json:"general"`
	Queries       models.Queries           `form:"queries" json:"queries"`
	Visualization *models.Visualization    `form:"visualization" json:"visualization"`
}

func NewTemplateResp(template *models.Template) *TemplateResp {
	resp := &TemplateResp{
		ID: template.ID,
	}
	if template.Alert.ValidateAlert() {
		resp.ShowSubscribe = true
	}
	general := new(request.TemplateGeneral)
	general.Name = template.Name
	general.Order = template.Order
	general.TypeID = template.TypeID
	general.Sla = template.Sla
	general.TypeName = template.TypeName
	//general.Sla = template.Sla
	if template.General != nil {
		general.Yaxes = template.General.Yaxes
	}
	resp.General = general
	resp.Queries = template.Queries
	resp.Alert = template.Alert
	resp.SubscribeInfo = new(request.SubscribeInfo)
	if template.SubscribeInfo != nil {
		resp.SubscribeInfo.SubscribeType = template.SubscribeInfo.SubscribeType
		resp.SubscribeInfo.Owner = template.SubscribeInfo.Owner
		resp.SubscribeInfo.Condition = &request.Condition{}
		if template.SubscribeInfo.Contain {
			resp.SubscribeInfo.Condition.Op = "contain" //包含返回前端contain不包含返回not_contain
		} else {
			resp.SubscribeInfo.Condition.Op = "not_contain"
		}
		resp.SubscribeInfo.Condition.Key = "level"
		resp.SubscribeInfo.Condition.Value = template.SubscribeInfo.Level
	}
	resp.Visualization = template.Visualization

	return resp
}
func NewTemplateResps(templates []*models.Template) []*TemplateResp {
	templateResps := make([]*TemplateResp, 0)
	for _, t := range templates {
		templateResps = append(templateResps, NewTemplateResp(t))
	}
	return templateResps
}

type GraphProgress struct {
	ID      uint64 `json:"id"`
	Total   int    `json:"total"`
	Current int    `json:"current"`
	Status  int    `json:"status"` // 0 排队中  1 进行中  2  已结束 3 失败
	Message string `json:"message"`
}
